#! /usr/local/bin/python
#-*- coding: utf-8 -*-

"""Interface de communication avec Tux Droid.
"""

"""
Minesweeper 3K. Original implementation of the classic game of mines
for Python 3.
Copyright (C) 2010  Cedric Bonhomme

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Minesweeper 3K  Copyright (C) 2010  Cedric Bonhomme
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
"""

__author__ = "Jerome Hussenet, Cedric Bonhomme"
__date__ = "$Date: 2010/02/27 $"
__copyright__ = "Copyright (c) 2010 Jerome Hussenet, Copyright (c) 2010 Cedric Bonhomme"
__license__ = "GPL 3"

try:
    from tuxisalive.api import *
except:
    raise Exception("Le module tuxisalive est manquant.")

class Tux(object):
    """Permet la communication avec Tux Droid.
    """
    def __init__(self, locutor = "Julie "):
        """ Connection au serveur.
        """
        self.tux = TuxAPI('127.0.0.1', 270)
        self.tux.server.autoConnect(CLIENT_LEVEL_RESTRICTED, 'none', 'none')
        print("Connexion avec Tux Droid...")

        if not self.tux.server.waitConnected(10.0):
            raise Exception("Le serveur Tux Droid n'est pas lancé.")
        print("Connecté avec le serveur Tux Droid.")

        if not self.tux.dongle.waitConnected(10.0):
            raise Exception("Le dongle n'est pas connecté.")
        print("Connecté avec le dongle.")

        if not self.tux.radio.waitConnected(10.0):
            raise Exception("Connexion entre le dongle et Tux Droid impossible.")
        print("Tux Droid connecté au dongle.")

        if self.tux.access.waitAcquire(10.0, ACCESS_PRIORITY_NORMAL):
            self.tux.tts.setLocutor(locutor) # set the locutor
            self.tux.tts.setPitch(110) # set the Pitch
            self.tux.tts.speakAsync("Salut ! Pret a jouer ?")

    def disconnect(self):
        """ Déconnexion du serveur.
        """
        self.tux.server.disconnect()
        self.tux.destroy()

    def win(self):
        """ Action en cas de victoire.
        """
        self.tux.flippers.onAsync(4, finalState = 'DOWN', speed = 3)
        self.tux.tts.speakAsync("Bravo ! Il n'y a plus de mines !")

    def loose(self):
        """ Action en cas de défaite.
        """
        self.tux.tts.speakAsync("Domage, tu as perdu. Retente ta chance !")

    def say_something(self, message):
        """ Fait parler Tux Droid.
        """
        self.tux.tts.speakAsync(message)
